#include "shop_2.h"
#include "ui_shop_2.h"

Shop_2::Shop_2(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Shop_2)
{
    ui->setupUi(this);

    m_status = new QLabel(this);
    m_sxt = new mysxt;

    setWindowTitle("伊玛楼兰新疆菜");

    m_tcpSock = new QTcpSocket(this);


    // 创建客户端的套接字对象并连接

    connect(m_tcpSock, &QTcpSocket::connected, this, [=]()
    {
        QPixmap pix(":/res/on.ico");
        pix = pix.scaled(20,20);
        m_status->setPixmap(pix);

        ui->bt_7->setEnabled(false);
        m_tcpSock->write("您有新的订单，请及时处理");

    });
    connect(m_tcpSock, &QTcpSocket::readyRead, this, [=]()
    {
      QByteArray ba = m_tcpSock->readAll();

      ui->te_3->append(ba);
    });
}

void Shop_2::recvStr(QString str)
{
    m_str = str;
}

void Shop_2::recvInt(int num)
{
    m_num = num;
}

void Shop_2::recvInt1(int num)
{
    m_num1 = num;
}

void Shop_2::recvInt2(int num)
{
    m_num2 = num;
}

void Shop_2::recvInt3(int num)
{
    m_num3 = num;
}

Shop_2::~Shop_2()
{
    delete ui;
}

void Shop_2::on_bt_1_clicked()
{

    while (i--)
    {
        QString str = QString("客户地址: %1 \n").arg(m_str);
        m_tcpSock->write(str.toUtf8());
        if(m_num1 != 0)
        {
            QString str = QString("客户点餐: %1 份手抓饭\n").arg(QString::number(m_num1));
            m_tcpSock->write(str.toUtf8());
        }
        if(m_num2 != 0)
        {
            QString str = QString("客户点餐: %1 份大盘鸡\n").arg(QString::number(m_num2));
            m_tcpSock->write(str.toUtf8());
        }
        if(m_num3 != 0)
        {
            QString str = QString("客户点餐: %1 串红柳烤串\n").arg(QString::number(m_num3));
            m_tcpSock->write(str.toUtf8());
        }
        QString str1 = QString("客户共消费 %1 元\n").arg(QString::number(m_num));
        m_tcpSock->write(str1.toUtf8());
    }

    QString msg = "客户:" + ui->te_2->toPlainText();
    m_tcpSock->write(msg.toUtf8());

    ui->te_3->append(msg);
    ui->te_2->clear();
}


void Shop_2::on_bt_7_clicked()
{
    QString ip = "127.0.0.1";
    m_tcpSock->connectToHost(ip, 8888);
}

void Shop_2::on_bt_6_clicked()
{
    m_tcpSock->close();
    int i = 1;
    this->close();
    emit sendInt(i);
}

void Shop_2::on_bt_4_clicked()
{
    m_sxt->show();
}
